
T.Events.LAYER_LOAD   = 'layerload'; //层载入开始
T.Events.LAYER_LOADED = 'layerloaded'; //层载入完成
T.Events.LAYER_OVERLAY_LOADED = 'layeroverlayloaded'; //Overlay载入完成


T.Layer = T.Evented.extend({
    
    /**
     * {ol.source}
     */
    source: null, 
    
    /**
     * {ol.layer}
     */
    layer: null,
    
    initialize: function () {
    }

    //公共方法
    ,addTo: function (map) {
        map.addLayer(this);
        return this;
    }

    ,remove: function () {
        return this.removeFrom(this._map || this._mapToAdd);
    }

    ,removeFrom: function (obj) {
        if (obj) {
            obj.removeLayer(this);
        }
        return this;
    }
    
    ,getPane: function (name) {
        return this._map.getPane(name ? (this.options[name] || name) : this.options.pane);
    }
    
    //子类重写
    ,onAdd: function(map){}

    //私有方法
    ,_initEvents: function () {
        var eventType = ['click', 'dblclick', 'moveend', 'pointerdrag', 'pointermove', 'singleclick'];
        for(var i in eventType){
            var et = eventType[i];
            var domEvent = T.bind(this._handleDOMEvent, this);
            this.tinyMap.on(et, domEvent);
        }
    }
    
    ,_layerAdd: function (e) {
        var map = e.target;

        this._map = map;
        
        this.onAdd(map);
        
        map.fire(T.Events.LAYER_LOADED, {layer: this});
    }
    
    ,_layerOverlayAdd: function (e) {
        var map = e.target;
        
        this._map = map;
        
        this.onAdd(map);
        
        map.fire(T.Events.LAYER_OVERLAY_LOADED, {layer: this});
    }
});


T.Map.include({
    addLayer: function (layer) {
        this.whenReady(layer._layerAdd, layer);
        return this;
    },
    
    addOverlay: function (layer) {
        this.whenReady(layer._layerOverlayAdd, layer);
        return this;
    },
    
    removeLayer: function (layer) {
        
    },

    eachLayer: function (method, context) {
       
    }
});
